package com.xique.system.controller;

import com.xique.common.core.domain.R;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.common.core.utils.poi.ExcelUtil;
import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.page.TableDataInfo;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.system.api.dto.SystemDictDataDTO;
import com.xique.system.bean.domain.SystemDictData;
import com.xique.system.service.biz.service.ISystemDictBizService;
import com.xique.system.service.service.ISystemDictDataService;
import com.xique.system.service.service.ISystemDictTypeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据字典信息
 *
 * @author xique
 */
@RestController
@RequestMapping("/dict/data")
public class SystemDictDataController extends BaseController
{
    @Autowired
    private ISystemDictDataService dictDataService;

    @Autowired
    private ISystemDictTypeService dictTypeService;

    @Autowired
    private ISystemDictBizService systemDictBizService;

    @PreAuthorize(hasPermi = "system:dict:list")
    @GetMapping("/list")
    public TableDataInfo list(SystemDictData dictData)
    {
        startPage();
        List<SystemDictData> list = dictDataService.selectDictDataList(dictData);
        return getDataTable(list);
    }

    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
    @PreAuthorize(hasPermi = "system:dict:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SystemDictData dictData) throws IOException
    {
        List<SystemDictData> list = dictDataService.selectDictDataList(dictData);
        ExcelUtil<SystemDictData> util = new ExcelUtil<SystemDictData>(SystemDictData.class);
        util.exportExcel(response, list, "字典数据");
    }

    /**
     * 查询字典数据详细
     */
    @PreAuthorize(hasPermi = "system:dict:query")
    @GetMapping(value = "/{dictCode}")
    public AjaxResult getInfo(@PathVariable Long dictCode)
    {
        return AjaxResult.success(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType)
    {
        List<SystemDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data))
        {
            data = new ArrayList<SystemDictData>();
        }
        return AjaxResult.success(data);
    }

    /**
     * 新增字典类型
     */
    @PreAuthorize(hasPermi = "system:dict:add")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SystemDictData dict)
    {
        dict.setCreateBy(SecurityUtils.getUsername());
        return toAjax(dictDataService.insertDictData(dict));
    }

    /**
     * 修改保存字典类型
     */
    @PreAuthorize(hasPermi = "system:dict:edit")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SystemDictData dict)
    {
        dict.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(dictDataService.updateDictData(dict));
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize(hasPermi = "system:dict:remove")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictCodes}")
    public AjaxResult remove(@PathVariable Long[] dictCodes)
    {
        dictDataService.deleteDictDataByIds(dictCodes);
        return success();
    }

    /**
     * 获取当前用户信息
     */
    @GetMapping("/label")
    @ApiOperation("获取字典信息")
    public R<String> label(@RequestParam("dictType") String dictType, @RequestParam("dictValue") String dictValue)
    {
        String dictLabel = dictDataService.selectDictLabel(dictType, dictValue);
        return R.ok(dictLabel);
    }

    /**
     * 根据字典类型获取字典信息
     * @param dictType
     * @return
     */
    @GetMapping(value = "/getDictDataByType/{dictType}")
    @ApiOperation("根据字典类型获取字典信息")
    public R<List<SystemDictDataDTO>> getDictDataByType(@PathVariable String dictType){
        return systemDictBizService.getDictDataByType(dictType);
    }

}
